package qc.module.qms.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.CommunicationChannelTypeEnum;
import qc.module.qms.dto.exchange.channel.ExchangeChannelDto;
import qc.module.qms.entity.ExchangeChannel;

import java.util.List;

/**
 * ExchangeChannelMapper
 *
 * @author QuCheng Tech
 * @since 2024/1/24
 */
@Mapper
public interface ExchangeChannelMapper {
    ExchangeChannelMapper MAPPER = Mappers.getMapper(ExchangeChannelMapper.class);

    @Mapping(target = "comtype", ignore = true)
    ExchangeChannel toEntity(ExchangeChannelDto dto);

    @Mapping(target = "comtype", source = "comtype.index")
    ExchangeChannelDto toDto(ExchangeChannel en);

    List<ExchangeChannelDto> toDtoList(List<ExchangeChannel> list);

    /**
     * DTO转换为Entity
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2024/4/16
     */
    default ExchangeChannel dtoToEntity(ExchangeChannelDto dto) {
        ExchangeChannel en = ExchangeChannelMapper.MAPPER.toEntity(dto);
        //处理数据类型，非空时进行转换，空值时使用默认
        Integer comtype = dto.getComtype();
        en.setComtype(getCommunicationChannelTypeEnum(comtype));
        return en;
    }

    static CommunicationChannelTypeEnum getCommunicationChannelTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (CommunicationChannelTypeEnum item : CommunicationChannelTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return CommunicationChannelTypeEnum.UN_DEFINE;
    }
}
